package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.*;
import com.elite.service.FileService;
import com.elite.service.GoodsService;
import com.elite.service.SellerService;
import com.elite.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2017/2/10.
 */
@RestController
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    private UserService userService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FileService fileService;
    @Autowired
    private HttpSession session;

    private static final Logger LOGGER = Logger.getLogger(String.valueOf(SchoolController.class));

    @RequestMapping(value = "goods",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String schoolGoods(String goodCategory){

        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        User user = (User) session.getAttribute("loginUser");
        Integer userId = user.getUserId();
 LOGGER.info("----------------------->>>>>>>>>>>."+goodCategory+","+userId);
        String userSchool = user.getUserSchool();

        Seller seller;
        List<Goods> goodses;
        List<Goods> goodses1 = new ArrayList<Goods>();
        List<Goods> goodses3 = new ArrayList<Goods>();
        List<Goods> finalGoods = new ArrayList<Goods>();
        try {
            seller = sellerService.findByUserId(userId);
            if (seller!=null){
                goodses = goodsService.selectGoodsBySellterId(seller.getSellerId());
                if (goodses!=null&&goodses.size()!=0){
                    for (Goods goods1 : goodses){
                        if (goods1.getGoodCategory().equals(goodCategory)&&userSchool.equals(goods1.getGoodsSchool())&&goods1.getGoodsSize()>0){
                            goodses1.add(goods1);
                        }
                    }
                }

            }

        }catch (NullPointerException e){
            e.printStackTrace();
        }


        try {
            List<Goods> goodses2 = goodsService.selectGoodsByGoodCategory(goodCategory);
            for (Goods goods2:goodses2){
                String goodSchool = goods2.getGoodsSchool();
                    if (userSchool.equals(goodSchool)&&goods2.getGoodsSize()>0){
                        goodses3.add(goods2);
                    }
            }
        }catch (NullPointerException e){
            e.printStackTrace();
        }

        if (goodses3.size() != 0){
            if (goodses1.size() != 0){
                for (Goods goods3:goodses3){
                    for (Goods goods4:goodses1){
                        if (!goods3.getGoodsId().equals(goods4.getGoodsId())){
                            finalGoods.add(goods3);
                        }
                    }
                }
            } else {
                finalGoods = goodses3;
            }

        }


        for (Goods goods5:finalGoods){
            List<FileGoods> fileGoodses = null;
            try{
            fileGoodses = goodsService.selectByGoodsId(goods5.getGoodsId());
            } catch (NullPointerException e){
            e.printStackTrace();
                 }
            int index = 0;
            if (fileGoodses != null && fileGoodses.size() != 0) {
            index = (int) (Math.random()*fileGoodses.size());
            }
            FileGoods fileGoods = null;
            if (fileGoodses != null && fileGoodses.size() != 0) {
            fileGoods = fileGoodses.get(index);
            }
            File file = null;
            if (fileGoods != null) {
            file = fileService.selectByPrimaryKey(fileGoods.getFileId());
            }
            if (file != null){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("goodsName",goods5.getGoodsName());
                map.put("goodsPrice",goods5.getGoodsPrice());
                map.put("goodsCity",goods5.getGoodsCity());
                map.put("goodsUnit",goods5.getGoodsUnit());
                map.put("goodsId",goods5.getGoodsId());
                map.put("fileUrl",file.getFileUrl());
                mapList.add(map);
            } else {
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("goodsName",goods5.getGoodsName());
                map.put("goodsPrice",goods5.getGoodsPrice());
                map.put("goodsCity",goods5.getGoodsCity());
                map.put("goodsUnit",goods5.getGoodsUnit());
                map.put("goodsId",goods5.getGoodsId());
                map.put("fileUrl","");
                mapList.add(map);
                  }
        }

    return JSON.toJSON(mapList).toString();
    }
}
